import 'package:flutter/material.dart';

enum Action { Ok, Cancel }

class AlertDialogDemo extends StatefulWidget {
  const AlertDialogDemo({super.key});

  @override
  State<AlertDialogDemo> createState() => _AlertDialogDemoState();
}

class _AlertDialogDemoState extends State<AlertDialogDemo> {
  String _choice = 'Nothing';

  _openAlertDialog() async {
    final option = await showDialog(
        context: context,
        barrierDismissible: false, //点击空白处 是否收回弹窗 默认true
        builder: (BuildContext buildContext) {
          return AlertDialog(
              title: Text('SimpleDialog'),
              content: Text('Are you sure about this?'),
              actions: [
                FlatButton(
                    onPressed: () {
                      Navigator.pop(context, Action.Cancel);
                    },
                    child: Text('Cancel')),
                FlatButton(
                    onPressed: () {
                      Navigator.pop(context, Action.Ok);
                    },
                    child: Text('Ok')),
              ]);
        });

    switch (option) {
      case Action.Ok:
        setState(() {
          _choice = "Ok";
        });

        break;
      case Action.Cancel:
        setState(() {
          _choice = "Cancel";
        });

        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SimpleDialogDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Your choice is $_choice'),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  child: Text('Open AlertDialog'),
                  onPressed: () {
                    _openAlertDialog();
                  },
                  style: ElevatedButton.styleFrom(
                      primary: Colors.blue,
                      onPrimary: Colors.red,
                      elevation: 4.0,
                      shape: StadiumBorder() //形状
                      ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}
